package com.example.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.OrientationHelper;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.domain.Student;

import java.util.ArrayList;
import java.util.List;

public class RecycleViewActivity extends AppCompatActivity {
    private RecyclerView rvStudent; // 学生循环器视图（展示）
    private RecyclerView.Adapter adapter; // 循环器适配器（桥梁）
    private List<Student> students; // 学生列表（数据源）

    @SuppressLint({"WrongConstant", "MissingInflatedId"})
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // 利用布局资源文件设置用户界面
        setContentView(R.layout.activity_recycle_view);

        // 通过资源标识符获取控件实例
        rvStudent = findViewById(R.id.rsStudent);

        // 获取学生列表作为数据源
        students = getStudents();

        // 创建线性布局管理器
        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        // 设置线性布局管理器方向属性（默认是VERTICAL）
        layoutManager.setOrientation(OrientationHelper.VERTICAL);
        // 给循环器视图设置布局管理器
        rvStudent.setLayoutManager(layoutManager);

        // 初始化循环器适配器
        adapter = new RecyclerView.Adapter() {

            @NonNull
            @Override
            public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                // 获取视图对象（将列表项模板转换成视图）
                View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.student_list_item, parent, false);
                // 获取视图里的控件
                ImageView ivStudentIcon = view.findViewById(R.id.ivStudentIcon);
                TextView tvStudentName = view.findViewById(R.id.tvStudentName);
                TextView tvStudentPhone = view.findViewById(R.id.tvStudentPhone);

                // 创建视图容器
                RecyclerView.ViewHolder viewHolder = new ViewHolder(view, ivStudentIcon, tvStudentName, tvStudentPhone);

                // 返回视图容器
                return viewHolder;
            }

            @Override
            public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
                // 获取学生数据
                Student student = students.get(position);

                // 获取控件实例
                ImageView ivStudentIcon = ((ViewHolder)holder).getIvStudentIcon();
                TextView tvStudentName = ((ViewHolder)holder).getTvStudentName();
                TextView tvStudentPhone = ((ViewHolder)holder).getTvStudentPhone();

                // 设置控件属性
                ivStudentIcon.setImageResource(student.getStudentIcon());
                tvStudentName.setText(student.getStudentName());
                tvStudentPhone.setText(student.getStudentPhone());

                // 给控件设置监听器
                ivStudentIcon.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(RecycleViewActivity.this, student.getStudentName()
                                + " : " + student.getStudentPhone(), Toast.LENGTH_SHORT).show();
                    }
                });
                tvStudentName.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(RecycleViewActivity.this, student.getStudentName()
                                + " : " + student.getStudentPhone(), Toast.LENGTH_SHORT).show();
                    }
                });
                tvStudentPhone.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(RecycleViewActivity.this, student.getStudentName()
                                + " : " + student.getStudentPhone(), Toast.LENGTH_SHORT).show();
                    }
                });
            }

            @Override
            public int getItemCount() {
                return students.size();
            }

            class ViewHolder extends RecyclerView.ViewHolder {
                private ImageView ivStudentIcon;
                private TextView tvStudentName;
                private TextView tvStudentPhone;

                public ViewHolder(@NonNull View itemView, ImageView ivStudentIcon, TextView tvStudentName, TextView tvStudentPhone) {
                    super(itemView);
                    this.ivStudentIcon = ivStudentIcon;
                    this.tvStudentName = tvStudentName;
                    this.tvStudentPhone = tvStudentPhone;
                }

                public ImageView getIvStudentIcon() {
                    return ivStudentIcon;
                }

                public TextView getTvStudentName() {
                    return tvStudentName;
                }

                public TextView getTvStudentPhone() {
                    return tvStudentPhone;
                }
            }
        };

        // 给循环器视图设置适配器
        rvStudent.setAdapter(adapter);

        // 给循环器视图添加列表项分隔线
        rvStudent.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
    }

    /**
     * @return 学生列表
     */
    public List<Student> getStudents() {
        // 创建学生列表
        List<Student> students = new ArrayList<>();
        // 声明学生
        Student student = null;

        // 创建第1个学生
        student = new Student();
        student.setStudentIcon(R.drawable.logo);
        student.setStudentName("李晓红");
        student.setStudentPhone("15878782345");
        // 将学生添加到学生列表
        students.add(student);

        // 创建第2个学生
        student = new Student();
        student.setStudentIcon(R.drawable.logo);
        student.setStudentName("王晓玲");
        student.setStudentPhone("15956567890");
        // 将学生添加到学生列表
        students.add(student);

        // 创建第3个学生
        student = new Student();
        student.setStudentIcon(R.drawable.logo);
        student.setStudentName("董大伟");
        student.setStudentPhone("13567891230");
        // 将学生添加到学生列表
        students.add(student);

        // 创建第4个学生
        student = new Student();
        student.setStudentIcon(R.drawable.logo);
        student.setStudentName("尚洪文");
        student.setStudentPhone("18856789032");
        // 将学生添加到学生列表
        students.add(student);

        // 创建第5个学生
        student = new Student();
        student.setStudentIcon(R.drawable.logo);
        student.setStudentName("唐语涵");
        student.setStudentPhone("15967893450");
        // 将学生添加到学生列表
        students.add(student);

        // 创建第6个学生
        student = new Student();
        student.setStudentIcon(R.drawable.logo);
        student.setStudentName("郑智化");
        student.setStudentPhone("15867678904");
        // 将学生添加到学生列表
        students.add(student);

        // 创建第7个学生
        student = new Student();
        student.setStudentIcon(R.drawable.logo);
        student.setStudentName("童安格");
        student.setStudentPhone("13845674560");
        // 将学生添加到学生列表
        students.add(student);

        // 返回学生列表
        return students;
    }
}